package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecification;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecificationItem;
import com.hilaver.dzmis.service.impl.BiMeasureSpecificationServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiMeasureSpecificationServlet extends AbstractBaseServlet {
	static final long serialVersionUID = 1L;

	public final static int ACTION_GET_MEASURE_SPECIFICATION_ITEM_ALL = 100; // "get one with description";
	public final static int ACTION_EDIT_MEASURE_SPECIFICATION_ITEM = 101; 
	public final static int ACTION_DELETE_MEASURE_SPECIFICATION_ITEM = 102;
	public final static int ACTION_GET_MEASURE_SPECIFICATION_ITEM = 103; 
	public final static int ACTION_GET_MAX_PATRONAGE_INDEX = 104;
	
	private BiMeasureSpecification biMS;
	private BiMeasureSpecificationItem biMSItem;
	private BiMeasureSpecificationServiceImpl msService;

	public BiMeasureSpecificationServlet() {
		super();
		this.simpleName = BiMeasureSpecification.class.getSimpleName();
		this.fullName = BiMeasureSpecification.class.getName();
		this.msService = new BiMeasureSpecificationServiceImpl();
		this.msService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.msService.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if(this.action == ACTION_EDIT || this.action == ACTION_EDIT_MEASURE_SPECIFICATION_ITEM){
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	public String execute(HttpServletRequest request) throws Exception {
		//this.setMeasureSpecificationParams(request);
		super.setAction(request);
		super.setAcUser(request, this.msService);
		String rtn = "";
		String ctStature = request.getParameter("ctStature");
		String ctCollarType = request.getParameter("ctCollarType");
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION:// "getAllPagination";
			super.setPaginationParam(request);
			rtn = this.msService.getAllPagination(this.page, this.sort,
					this.dir);
			break;
		case ACTION_EDIT: // "edit";
			//this.setMeasureSpecificationParams(request);
			this.fillObj(request);
			this.biMS = (BiMeasureSpecification)this.obj;
			rtn = this.msService.edit(this.biMS);
			break;
		case ACTION_DELETE: // "delete";
			super.setId(request);
			rtn = this.msService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			super.setId(request);
			rtn = this.msService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			super.setPaginationParam(request);
			super.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			List<String> filterList = new ArrayList<String>();
			for(int i = 0; i < this.filters.length; i ++){
				filterList.add(this.filters[i]);
			}
			if(ctCollarType != null){
				filterList.add("ctCollarType='" + ctCollarType + "'");
			}
			if(ctStature != null){
				filterList.add("ctStature='" + ctStature + "'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			rtn = this.msService.getAllPagination(this.page, this.sort,
					this.dir, super.filters);
			break;
		case ACTION_GET_ALL: // Get all
			break;

		case ACTION_GET_WITH_DESCRIPTION: // get one with codetable descriptoin.
			super.setId(request);
			rtn = this.msService.getWithDescription(this.id);
			break;

		case ACTION_GET_MEASURE_SPECIFICATION_ITEM_ALL:
			super.setId(request);
			rtn = this.msService.getItemAll(this.id);
			break;
		case ACTION_EDIT_MEASURE_SPECIFICATION_ITEM:
			super.setId(request);
			this.fillMSItemObj(request);
			rtn = this.msService.edit(this.biMSItem);
			break;
		case ACTION_DELETE_MEASURE_SPECIFICATION_ITEM:
			super.setId(request);
			rtn = this.msService.deleteItem(this.id);
			break;
		case ACTION_GET_MEASURE_SPECIFICATION_ITEM:
			super.setId(request);
			rtn = this.msService.getItem(this.id);
			break;
			
		case ACTION_GET_MAX_PATRONAGE_INDEX:
			rtn = this.msService.getMaxPatronageIndex(ctStature, ctCollarType);
			break;
		}

		return rtn;
	}
	
	private void fillMSItemObj(HttpServletRequest request) throws Exception {
		this.fillObj(BiMeasureSpecificationItem.class.getName(), request);
		this.biMSItem = (BiMeasureSpecificationItem)this.obj;
		this.biMSItem.setBiMeasureSpecification(new BiMeasureSpecification());
		String msId = (String)this.getValue(request, "msId");
		this.biMSItem.getBiMeasureSpecification().setId(Integer.parseInt(msId));
	}
}
